package g2.routemaster.gui.wizards;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class AddPlayerPage extends WizardPage implements Listener {
   private Text tName;
   private Text tAge;
   private Text tColor;
   
   protected AddPlayerPage() {
      super("newPlayerPage");
      setTitle("Player");
      setDescription("Specify player related data");
   }
   
   public void createControl(Composite parent) {
      // create the composite to hold the widgets
      //GridData gd;
      Composite composite = new Composite(parent, SWT.NONE);

      // create the desired layout for this wizard page
      GridLayout gl = new GridLayout();
      int ncol = 3;
      gl.numColumns = ncol;
      composite.setLayout(gl);

      new Label(composite, SWT.NONE).setText("Name:");
      tName = new Text(composite, SWT.BORDER | SWT.SINGLE);
      tName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      tName.addListener(SWT.Modify, this);
      new Label(composite, SWT.NONE);
      
      new Label(composite, SWT.NONE).setText("Age:");
      tAge = new Text(composite, SWT.BORDER | SWT.SINGLE);
      tAge.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      tAge.addListener(SWT.Modify, this);
      new Label(composite, SWT.NONE);
      
      new Label(composite, SWT.NONE).setText("Color:");
      tColor = new Text(composite, SWT.BORDER | SWT.SINGLE);
      tColor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
      tColor.addListener(SWT.Modify, this);
      tColor.setEditable(false);
      Button bChooseColor = new Button(composite, SWT.PUSH);
      bChooseColor.setText("Choose ...");
      bChooseColor.addListener(SWT.Selection, new Listener(){
         public void handleEvent(Event e){
            ColorDialog dial = new ColorDialog(getShell());
            RGB color = dial.open();
            if(color != null){
               tColor.setBackground(new Color(getShell().getDisplay(), color));
               tColor.setText("("+color.red+","+color.green+","+color.blue+")");
               if(color.red + color.green + color.blue > 3*128){
                  tColor.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_BLACK));
               } else {
                  tColor.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_WHITE));
               }
            }
         }
      });
      
      // set the composite as the control for this page
      setControl(composite);
      setPageComplete(false);
   }
   
   public boolean canFlipToNextPage(){
      return false;
   }
   
   public void handleEvent(Event e){
      setPageComplete(isPageComplete());
      getWizard().getContainer().updateButtons();
   }
   
   public boolean isPageComplete(){
      AddPlayerWizard wizard = (AddPlayerWizard)getWizard();
      
      Status status = validate();
      if (status.getSeverity() == IStatus.ERROR) {
         wizard.isCompleted = false;
         setErrorMessage(status.getMessage());
         return false;
      } else {
         setErrorMessage(null);
      }
      
      saveDataToModel();
      return true;
   }
   
   Status validate(){
      Status status = validateName(tName.getText());
      if(status == null){
         status = validateAge(tAge.getText());
      }
      if(status == null){
         status = validateColor(tColor.getText());
      }
      
      if(status != null){
         return status;
      } else {
         return new Status(IStatus.OK, "not_used", 0, "", null);
      }
   }
   
   Status validateName(String name){
      if(name == null || name.length() == 0){
         return new Status(IStatus.ERROR, "not_used", 0,
               "Name can't be empty.", null);
      }
      return null;
   }
   
   Status validateAge(String age){
      if(age == null || age.length() == 0){
         return new Status(IStatus.ERROR, "not_used", 0,
               "Age can't be empty.", null);
      } 
      try {
         Integer.parseInt(age);
      } catch (NumberFormatException e){
         return new Status(IStatus.ERROR, "not_used", 0,
               "Age must be a numeric.", null);
      } 
      
      return null;
   }
   
   Status validateColor(String color){
      if(color == null || color.length() == 0){
         return new Status(IStatus.ERROR, "not_used", 0,
               "Color should be chosen.", null);
      }
      return null;
   }
   
   private void saveDataToModel(){
      AddPlayerWizard wizard = (AddPlayerWizard)getWizard();
      
      wizard.playerName = tName.getText();
      wizard.playerAge = Integer.parseInt(tAge.getText());
      wizard.playerColor = tColor.getText();
      
      wizard.isCompleted = true;
   }
   
   void onEnterPage(){
      AddPlayerWizard wizard = (AddPlayerWizard)getWizard();
      wizard.isCompleted = false;
   }
}
